function include_js(path) 
{     
	document.write("<scr"+"ipt language=javascript src='"+path+"'></scr"+"ipt>");
};
include_js("js/others/sylvester.js");
include_js("js/custom/Camera.js");
include_js("js/custom/Model.js");
include_js("js/custom/SandBoxControler.js");

function Room(){
	this.container=null;//div container
	this.stats=null;//small div on right-top to show stats
	this.scene=null;
	this.webglRenderer=null;
	
    this.group=null;
    
    this.roomCamera=null;
    this.roomModelManager=null;
    this.roomSandBoxControler=null;
    
    Room.self_ptr=this;
}
//static attribute
Room.self_ptr=null;
//prototype
Room.prototype = {
		constructor : Room,
		createModel:function(){
			//draw the cordinate system space
			this.drawCordinateSpace(this.scene);
			
			this.group = new THREE.Object3D();
			//Room
			var myRoom=new Model();
            myRoom.loadToGroup('models/room/newRoom.js', this.group);	
            myRoom.receiveShadow=true;
            this.roomModelManager.add(myRoom);
            
            //Desk
            var myDesk=new Model();
            myDesk.loadToGroup('models/apple_obj/apple_pc.js', this.group);	
            this.roomModelManager.add(myDesk);
            //Apples
            var myApples=new Model();
            myApples.loadToGroup('models/my_apple_obj/apple.js',this.group);
            this.roomModelManager.add(myApples);
			//Bed 
            var myBed=new Model();
            myBed.loadToGroup('models/bed/bed1.js',this.group);
            myBed.castShadow=true;
            this.roomModelManager.add(myBed);
            
            //GlassTable
            var myGlassTable=new Model();
            myGlassTable.loadToGroup('models/glass_table/metal_glass_table.js', this.group);
            myGlassTable.transparent=true;
            this.roomModelManager.add(myGlassTable);
            
            //put the group in scene
            this.scene.add( this.group );
            
            this.roomSandBoxControler=new SandBoxControler(this.roomCamera);
            document.addEventListener( 'keydown', this.roomSandBoxControler.onDocumentKeyDown, false );
			document.addEventListener( 'keyup', this.roomSandBoxControler.onDocumentKeyUp, false );
		},
		lightInit:function(){
			 //ambientLight
			ambientLight = new THREE.AmbientLight( 0x000000 );
           	
			//spotLight
           	var spotLight = new THREE.SpotLight( 0xFFAA88, 2 );
           	spotLight.target.position.set( 0, 5, 0 );
           	spotLight.shadowCameraNear		= 0.01;		
			spotLight.position.set( -20, 20, 20 );
			spotLight.castShadow		= true;
			spotLight.shadowDarkness		= 0.5;
			
			
			spotLight.shadowCameraNear = 10;
			spotLight.shadowCameraFar = 50;
			spotLight.shadowCameraFov = 50;
			spotLight.shadowCameraVisible	= true;
            //directionalLight
           directionalLight = new THREE.DirectionalLight( 0xffffff );
           directionalLight.intensity = 1.5;
           directionalLight.position.x = 10;
           directionalLight.position.y = 10;
           directionalLight.position.z = 13;
           directionalLight.position.normalize();
           directionalLight.shadowCameraVisible	= true;
           //put light in scene
          this.scene.add( ambientLight);
          this.scene.add(spotLight);
          //this.scene.add(directionalLight);  
		},
		//call in createModel()
		drawCordinateSpace: function(scene){
        	//x-axis
        	var geometry = new THREE.Geometry();
			var vector = new THREE.Vector3( 100, 0, 0 );
			geometry.vertices.push( new THREE.Vertex( vector ) );
			var vector2 = new THREE.Vector3( -100, 0, 0 );
			geometry.vertices.push( new THREE.Vertex( vector2 ) );
			var lineX = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0x00ff00 , opacity: 0.5 } ) );
			scene.add(lineX);
			//y-axis
			geometry = new THREE.Geometry();
			vector = new THREE.Vector3( 0, 100, 0 );
			geometry.vertices.push( new THREE.Vertex( vector ) );
			vector2 = new THREE.Vector3( 0, -100, 0 );
			geometry.vertices.push( new THREE.Vertex( vector2 ) );
			var lineY = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0x0000ff ,opacity: 0.5 } ) );
			scene.add(lineY);
			//z-axis
			geometry = new THREE.Geometry();
			vector = new THREE.Vector3( 0, 0, 100 );
			geometry.vertices.push( new THREE.Vertex( vector ) );
			vector2 = new THREE.Vector3( 0, 0, -100 );
			geometry.vertices.push( new THREE.Vertex( vector2 ) );
			var lineZ = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0xff0000 , opacity: 0.5  } ) );
			scene.add(lineZ);
			
			//the x-z face griding
			var side=50;
			//x-axis line
            for(var i=0;i<=side;i++){
            	geometry = new THREE.Geometry();
    			vector = new THREE.Vector3( -side/2, 0, -side/2+i );
    			geometry.vertices.push( new THREE.Vertex( vector ) );
    			vector2 = new THREE.Vector3(side/2, 0, -side/2+i );
    			geometry.vertices.push( new THREE.Vertex( vector2 ) );
    			var lineZ = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0x6e6e6e , opacity: 0.5  } ) );
    			scene.add(lineZ);
            }
            //z-axis line
            for(var i=0;i<=side;i++){
            	geometry = new THREE.Geometry();
    			vector = new THREE.Vector3( -side/2+i, 0, -side/2 );
    			geometry.vertices.push( new THREE.Vertex( vector ) );
    			vector2 = new THREE.Vector3(-side/2+i, 0, side/2 );
    			geometry.vertices.push( new THREE.Vertex( vector2 ) );
    			var lineZ = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0x6e6e6e , opacity: 0.5  } ) );
    			scene.add(lineZ);
            }
        },
        onStart:function(){
			
			//little stat window
			this.stats= new Stats();
			this.stats.domElement.style.position = 'absolute';
			this.stats.domElement.style.top = '0px';
			//set camera
			this.roomCamera=new Camera();
            this.roomCamera.init( 0, 0,10,new THREE.Vector3(0,0,0));
			//initial scene
			this.scene = new THREE.Scene();
			//
			this.lightInit();
			
			//initial webglRenderer
            this.webglRenderer = new THREE.WebGLRenderer();
            this.webglRenderer.setSize( window.innerWidth, window.innerHeight );
            this.webglRenderer.setFaceCulling( 0 );
            this.webglRenderer.shadowMapEnabled = true;//enable shadow 
            this.webglRenderer.shadowMapSoft = true;
			//create container and make it to the body
			this.container=document.createElement( 'div' );
			document.body.appendChild(this.container);
			//put the stats div and the canvas to the container
			this.container.appendChild( this.webglRenderer.domElement );
			this.container.appendChild( this.stats.domElement );		
			
			//new Model Manager
			this.roomModelManager=new ModelManager();
			
			//here will be change later
			//create model
		
			this.createModel();			
		},
    	onRender:function() {
            this.webglRenderer.render( this.scene, this.roomCamera.camera );
        },
		onUpdate:function() {
			this.stats.update();
            this.roomModelManager.update();
            this.roomSandBoxControler.update();
            this.roomCamera.update();
		},
		onAnimate:function() {
			requestAnimationFrame( Room.self_ptr.onAnimate );
			Room.self_ptr.onUpdate();
			Room.self_ptr.onRender();
		},
		
		excute:function(){
			this.onStart();
			this.onAnimate();
		}
};




	
